
var address_book = liwe.module ( "address_book" );

address_book._dest_div = null;
address_book.profile = null;
address_book.kind_list = null;
address_book._loc_opts = {};
address_book._category_opts = [];
address_book._category_cache = {};
address_book._panel_id = null;
address_book._panel_label = "Indirizzi";


address_book.init = function ()
{
	address_book.valid_events ( [ 'noprofile', 'setprofile', 'close', 'seladdress', 'render_profile', 'render_address' ] );
	address_book.load_templates ();
	address_book.get_kind_list ();
	
	//EVENTS
	user.addEventListener ( 'profile', address_book.user_profile );
	user.addEventListener ( 'add_box' , function ( e )
	{
		var data = e.params;
		if ( data.module == "address_book" ) address_book._panel_id = data.id_panel;
	} );
	address_book.addEventListener ( 'setprofile', function ( evt )
	{
		var p = evt.params;
		if ( p [ 'module' ] == 'address_book' ) 
			address_book.set_profile;
	}, true );
};

address_book.set_profile = function ()
{
	address_book._format_profile_data ( function ( content, check, data )
	{
		$( address_book._panel_id, content )
		address_book.render_address ( "address_book_profile_address", "address_book", data );
	} );
}

address_book.user_profile = function ( e )
{
	address_book._format_profile_data ( function ( content, check, data )
	{
		user.profile_add_box ( content, "address_book", address_book._panel_id, address_book._panel_label , address_book.profile );
		if ( ! check ) address_book.edit_profile ( "address_book_profile_form" );
		if ( check ) address_book.render_address ( "address_book_profile_address", "address_book", data )
	} ); 
};


address_book._format_profile_data = function ( cback )
{
	address_book.load_templates ( function ()
	{
		var get =  { 'action' : 'address_book.ajax.get_info', 'address' : 1 };
		var _dct = dict( { '_data' : '', '_address' :  address_book.templates [ "SITE_USER_PROFILE_ADDRESS" ] } );
		address_book.ajax ( get , function ( data )
		{
			_dct [ '_data' ] =  String.formatDict ( address_book.templates [ 'SITE_USER_PROFILE_DATA' ] , data.profile ) ;
			var content = String.formatDict ( address_book.templates [ 'SITE_USER_PROFILE' ] ,  _dct );
			cback ( content, true, data );
		},function ( txt, err_descr, err_code, data )
		{		
			var content = String.formatDict ( address_book.templates [ 'SITE_USER_PROFILE' ] , _dct );
			cback ( content, false, data )
		} );
	} );

}

address_book.user_profile_form = function ()
{
	$( 'address_book_profile_data', "" );           
        address_book.render_profile ( 'address_book_profile_form' )
};


//{{{address_book.render_profile = function ( dest_div )
address_book.render_profile = function ( dest_div, module, minify )
{

	var dest_div = dest_div || address_book._dest_div;
	var module = module || "address_book";
	if ( typeof ( minify ) == "undefined" ) minify = false;
	
	
	var get =  { 'action' : 'address_book.ajax.get_info', 'address' : 1 };
	address_book.ajax ( get , function ( res )
	{
		address_book.profile =  res.get ( 'profile', {} );
		address_book.profile [ 'module' ] = module;
		address_book.profile [ 'minify' ] = minify;
		address_book.edit_profile ( dest_div, address_book.profile );
		address_book.dispatchEvent ( "render_profile", null, address_book.profile );
		
	},function ( txt, err_descr, err_code, data )
	{
		if ( ! err_descr ) err_descr = txt;
		data [ 'module' ] = module;
		data [ 'minify' ] = minify;
		address_book.dispatchEvent ( "noprofile", null, data );
	} );
}
//}}}
address_book.edit_profile = function ( dest_div, data )
{
	if ( typeof ( dest_div ) == "undefined" )	dest_div = address_book._dest_div;
	if ( typeof ( data ) == "undefined" ) data = {};
	$( dest_div, address_book.templates [ 'SITE_PROFILE_BOX' ] );
	address_book.get_location_opts ( function ()
	{
		address_book._make_profile_form ( data );
	}, null , data );
}


//{{{address_book.render_address = function ( dest_div )
address_book.render_address = function ( dest_div, module, profile_data )
{
	var dest_div = dest_div || address_book._dest_div;
	var module = module || "address_book";

	address_book._dest_div = dest_div;

	var render = function ( data )
	{
		var dct = { 'module' : module };
		$( dest_div, String.formatDict ( address_book.templates [ 'SITE_ADDRESS_BOX' ], dct ) );
		data.profile [ '_mod_link' ] = '';
		data.profile [ 'kind_name' ] = "Primario";
		$( 'profile_address_default' , String.formatDict ( address_book.templates [ 'SITE_ADDRESS_LIST_ITEM' ], data.profile ) ); 				
	
		address_book.profile =  data.get ( 'profile', {} );
		address_book.dispatchEvent ( "render_address", null, data.profile.address.rows );
		if ( data.profile.address.tot_rows == 0 ) { return; };
		var html_address = address_book._render_address_list ( data.profile.address.rows );
		$ ( 'profile_address_list', html_address );	
	} ;

	if ( typeof ( profile_data ) == 'undefined'  )
	{
		var post =  { 'action' : 'address_book.ajax.get_info', 'address' : 1 };
		address_book.ajax ( post , function ( res )
		{
			render ( res );
		},function ( txt, err_descr, err_code, data )
		{
			if ( ! err_descr ) err_descr = txt;
			address_book.dispatchEvent ( "nologin", null, { 'alert' : err_descr } );
		} );
	} else { 
		render ( profile_data );
	};
	
	

};
//}}}
//{{{address_book.address_box = function ( dest_div, id_address, title )
address_book.address_box = function ( dest_div, id_address, title )
{
var get =  { 'action' : 'address_book.ajax.get_address', 'id_address' : id_address, 'get_default' : 1 };
address_book.ajax ( get , function ( res )
{
	var v =  res.address;
	v [ 'kind_name' ] = title;
	v [ '_mod_link' ] = '';
	$ ( dest_div , String.formatDict ( address_book.templates [ "SITE_ADDRESS_LIST_ITEM" ] , v ) );	
} );

}; 
//}}}
//{{{address_book._make_profile_form = function ( data )
address_book._make_profile_form = function ( data )
{
	to_dict ( data );
	var f = new liwe.form5.instance ( 'profile_form' );
	f.action = 'address_book.ajax.set_profile';
	f.add ( "hidden", "email", { value :  data.get ( 'email' , '' ) } );
	f.add ( "hidden", "module", { value :  data.get ( 'module' ) } );
	f.add ( "hidden", "minify", { value :  data.get ( 'minify' ) } );
	f.add ( "hidden", "company", { value :  data.get ( 'company' ) } );
	f.add ( "hidden", "individual", { value :  data.get ( 'individual' ) } );

	f.add ( "text", 'name', { label : 'Nome/Ragione Sociale', value : data.get ( 'name' , '' ), size : 30, 'maxlength' : 30, mandatory : true } );
	f.add ( "text", 'lastname', { label : 'Cognome', value : data.get ( 'lastname' , '' ), size : 30, 'maxlength' : 30, mandatory : true } );
	if ( ! eval ( data  [ 'minify' ]  ) )
	{
		var kind_opts = [ 
				{ value : 1, label : "Persona Fisica" },
				{ value : 2, label : "Azienda" }
		]
		var kind_val =  1;
		if ( data.get ( 'company', 0 ) ) kind_val = 2;
		f.add ( "select", "subject_kind" , 
							{ 	label : "Soggetto", 
								options  : kind_opts , 
								value : kind_val, 
								onchange :  "address_book.set_subject_kind ( this, address_book._make_profile_form, 'profile_form')"
							} ); 
		if( kind_val == 2 )
		{
			f.add ( "text", "vat_code", { label : "P:IVA", filter: "1234567890",  value : data.get ( 'vat_code' , '' ) , size : 30, 'maxlength' : 20, 'width' : '100%', mandatory : true } );
		};
		f.add ( "cod_fisc", "tax_code", { label : 'Cod. Fiscale', size : 30, 'maxlength' : 20,
			'width' : '100%', mandatory : true, value : data.get ( 'tax_code' , '' ), onblur : "address_book.check_form_value( this, 'cfisc')" } );
		f.add ( "text", "phone", { 'label' : 'Telefono', size : 10, 'maxlength' : 50, filter: "1234567890 +.", value : data.get ( 'phone' , '' ) } );
		f.add ( "text", "cell", { 'label' : 'Cellulare', size : 10, 'maxlength' : 50, filter: "1234567890 +.", value : data.get ( 'cell' , '' ) } );
		f.add ( "text", "fax", { 'label' : 'FAX', size : 10, 'maxlength' : 50,  filter: "1234567890 +.",value : data.get ( 'fax' , '' ) } );
		f.add ( "select", "country", { 'label' : 'Stato',
			 'options' : address_book._loc_opts [ 'stati' ],
			 force_select : true ,
			 onchange: "address_book.change_stato( this, address_book._make_profile_form, 'profile_form')",
			 value : data.get ( 'country' , 'Italia' )} );
		
		if ( data.get ( 'country', 'Italia' ) == 'Italia' )
		{
			f.add ( "select", 'prov',  { 'label' : 'Provincia', 
						 'options' : address_book._loc_opts  [ 'province' ],
						 'force_select' : true,
						 mandatory : true, 
						 onchange: "address_book.change_prov( this, address_book._make_profile_form, 'profile_form')", 
						 value : data.get ( 'prov' , '' ), force_select: true } );

			if ( data.get ( 'prov' ) )
			{
				f.add ( "select", "city", {  label: "Citt&agrave;", 
							 'force_select' : true,
							 mandatory : true, 
							 options: address_book._loc_opts [ data.prov ], 
							 onchange: "address_book.change_comu( this, address_book._make_profile_form, 'profile_form')", 
							 value: data.get ( 'city', '' ), force_select: true } );
			};
			if ( data.get ( 'city' ) )
			{
			
				f.add ( "select", "zip_code",  { label: "C.A.P.", mandatory : true, options: address_book._loc_opts [ data.prov + data.city ], value: data.get ( 'zip_code', '' ) } );
			};
			f.add ( "text", "addr1", { 'label' : 'Indirizzo', mandatory : true, 'size' : 30 , 'maxlength' : 50, 'colspan' : 5, value : data.get ( 'addr1', '' ) } );
		} else {
			f.add ( "textarea", "addr1",  { 'label' : 'Indirizzo', 'rows' : 5 , 'cols' : 50, value : data.get ( 'addr1', '' ) } );
		};
	};
	f.add ( "submit", "invia", { value : "Salva", "class" : "button" , ignore :true } );
	f.addEventListener ( "complete", address_book.profile_save );
	f.addEventListener ( "check-error", function ( evt )
	{
		var s = new String.buffer ();
		var err = evt.params;

		if ( err [ 'mandatory' ].count () )
		{
			s.add ( "Campi obbligatori mancanti\n\n" );
			err [ 'mandatory' ].iterate ( function ( item, k )
			{
				s.add ( item [ 'label' ] + "\n" );
			} );
		}

		if ( err [ 'validate' ].count () )
		{
			s.add ( "Campi con errata validazione\n\n" );
			err [ 'validate' ].iterate ( function ( item, k )
			{
				s.add ( item [ 'label' ] + "\n" );
			} );
		}

		alert ( s.toString () );
		return false;
	} );
	f.set ( 'profile_form', address_book.templates [ "PROFILE-FORM" ] );
}
//}}}
address_book.check_form_value = function( el, kind )
{
	switch ( kind )
	{
		case  'cfisc':
			if ( ! liwe.validators.is_codice_fiscale ( el.value ) ) alert ( "Codice fiscale non valido" );	
			break;
	}
}
//{{{address_book.profile_save = function ( resp)
address_book.profile_save = function ( resp )
{
	var f = liwe.form.get ( "profile_form" );
	if ( typeof ( f ) == "undefined" ) f = liwe.form5.get ( "profile_form" );	//FIX FOR FORM5
	if ( typeof ( resp.profile ) == 'undefined' )  resp = resp.params.response; //FIX FOR FORM5

	var vals = f.get_values ();
	vals [ 'uid' ] = resp.profile [ 'uid' ];
	address_book.dispatchEvent ( "setprofile", null, vals );
}
//}}}
//{{{address_book.add_address = function ()
address_book.add_address = function ( address, mode, module )
{
	var mode = mode || "standard";
	var module = module || "address_book";
	//address_book._form_mode = mode

	var load_form = function ( data )
	{
		liwe.types.to_dict ( data );
		if ( ! data.get ( 'mode' ) ) data [ 'mode' ] = mode;
		if ( ! data.get ( 'module' ) ) data [ 'module' ] = module;
		address_book.get_location_opts ( function ()
		{
			address_book._make_address_form ( data );
		}, null , data  );
		return;
	};

	if ( typeof ( address ) == 'object' && address != null  )  { return  load_form ( address); };

	if ( address_book.profile )
	{ 
		address_book.profile [ 'mode' ] = mode;
		address_book.profile [ 'module' ] = module;
		return  load_form ( address_book.profile );
	};
	
	var get =  { 'action' : 'address_book.ajax.get_info' };
	return address_book.ajax ( get, function ( res )
	{
		address_book.profile =  res.get ( 'profile', {} );
		address_book.profile [ 'mode' ] = mode;
		address_book.profile [ 'module' ] = module;
		return  load_form ( address_book.profile );
	} );
}
//}}}
//{{{address_book.set_address = function ( id_address, mode, module )
address_book.set_address = function ( id_address, mode, module )
{
	var mode = mode || "standard";
	var module = module || "address_book";

	var get = { 'action' : 'address_book.ajax.get_address' , 'id_address' : id_address }
	address_book.ajax ( get, function ( res )
	{
		res.address [ 'mode' ] = mode;
		res.address [ 'module' ] = module;
		address_book.add_address ( res.address );
	} ) ;
}
//}}}
//{{{address_book._make_address_form = function ( data )
address_book._make_address_form = function ( data )
{
	to_dict ( data );
	var f = new liwe.form5.instance ( 'address_form' );

	f.action = 'address_book.ajax.set_address';

	f.add ( "hidden", "mode", { value :  data.get ( 'mode' , '' ) } );
	f.add ( "hidden", "module", { value :  data.get ( 'module' ) } );
	f.add ( "hidden", "id_address" , {  value :  data.get ( 'id', data.get ( 'id_address' ), 0 ) } );
	f.add ( "hidden", "id_profile", { value :  data.get ( 'id_profile' , 0 ) } );

	if ( address_book.kind_opts().count() )	f.add ( "select", "id_addr_kind", { 'label' : 'Tipo', 'options' : address_book.kind_opts(), value : data.get ( 'id_addr_kind', 0 ) } );
	f.add ( "select", "country", { 'label' : 'Stato',
		 'options' : address_book._loc_opts [ 'stati' ],
		 force_select : true ,
		 mandatory:true,
		 onchange: "address_book.change_stato( this, address_book._make_address_form, 'address_form')",
		 value : data.get ( 'country' , 'Italia' )} );
		
	if ( data.get ( 'country', 'Italia' ) == 'Italia' )
	{
		f.add ( "select", 'prov',  { 'label' : 'Provincia', 
					 'options' : address_book._loc_opts  [ 'province' ], 
					 mandatory:true,
					 onchange: "address_book.change_prov( this, address_book._make_address_form, 'address_form')", 
					 value : data.get ( 'prov' , '' ), force_select: true } );

		if ( data.get ( 'prov' ) )
		{
			f.add ( "select", "city", {  label: "Citt&agrave;", 
						 options: address_book._loc_opts [ data.prov ], 
						 mandatory:true,
						 onchange: "address_book.change_comu( this, address_book._make_address_form, 'address_form')", 
						 value: data.get ( 'city', '' ), force_select: true } );
		};
		if ( data.get ( 'city' ) )
		{
		
			f.add ( "select", "zip_code",  { label: "C.A.P.",mandatory : true, options: address_book._loc_opts [ data.prov + data.city ], value: data.get ( 'zip_code', '' ) } );
		};
		f.add ( "text", "addr1", { 'label' : 'Indirizzo', 'size' : 30 , mandatory : true,  'maxlength' : 50, 'colspan' : 5, value : data.get ( 'addr1', '' ) } );
		f.add ( "text", "addr2", { 'label' : 'Indirizzo secondario', size : 30, 'maxlength' : 50, 'colspan' : 6, value : data.get ( 'addr2', '' ) } );
	} else {
		f.add ( "textarea", "addr1",  { 'label' : 'Indirizzo', mandatory : true,'rows' : 5 , 'cols' : 50, value : data.get ( 'addr1', '' ) } );
		f.add ( "textarea", "addr2", { 'label' : 'Indirizzo secondario', size : 30, 'maxlength' : 50, 'colspan' : 6, value : data.get ( 'addr2', '' ) } );
	};

	f.add ( "text", "cell", { 'label' : 'Cellulare', size : 10, 'maxlength' : 50, filter: "1234567890 +.", value : data.get ( 'cell' , '' ) } );
	f.add ( "email", "e-mail", { 'label' : 'e-mail', size : 10, 'maxlength' : 50,  value : data.get ( 'mail' , '' ) } );

	f.add ( "submit", "invia", { value : "Salva", "class" : "button" , ignore :true } );
	f.addEventListener ( "complete", address_book.save_address );
	f.set ( "profile_address_form", address_book.templates [ "ADDRESS-FORM" ] );
};
//}}}
address_book.address_form_close= function()
{
	$("profile_address_form", "" );
};
//{{{address_book.save_address = function ()
address_book.save_address = function ()
{
	var f = liwe.form.get ( "address_form" );
	if ( typeof ( f ) == "undefined" ) f = liwe.form5.get ( "address_form" );	//FIX FOR FORM5
	var vals = f.get_values ();
	
	if ( vals [ 'mode' ] == 'select' )
	{
		address_book.select_address_box ( address_book.select_dest_div, address_book.select_kind, vals [ 'module' ] );
		return;
	}
	address_book.render_address ( address_book._dest_div, vals [ 'module' ] )
}
//}}}
//{{{address_book._render_address_list = function  ( address_list )
address_book._render_address_list = function  ( address_list )
{
	liwe.types.to_list ( address_list );
	var buf = new String.buffer();
	address_list.iterate ( function ( el )
	{
		el [ '_mod_link' ] = String.formatDict ( address_book.templates [ 'SITE_ADDRESS_LIST_MODIFY' ], el );
		if ( el.kind_name == null ) el.kind_name = "";
		
		buf.add ( String.formatDict ( address_book.templates [ 'SITE_ADDRESS_LIST_ITEM' ], el ) )
	} );
	return buf.toString();

}
//}}}
//{{{address_book.close = function ( module )
address_book.close = function ( module )
{
	var dct = { 'module' : module };
	address_book.dispatchEvent ( "close", null, dct );
};
//}}}
//{{{address_book.select_address_box = function  ( dest_div, kind, module )
address_book.select_address_box = function  ( dest_div, kind, module )
{
	address_book.select_kind =  kind;
	address_book.select_dest_div =  dest_div;
	var get = { 'action' : 'address_book.ajax.address_select' };
	address_book.ajax ( get, function ( resp )
	{
		var rows =  resp [ 'rows' ];
		liwe.types.to_list ( rows );
		var buf = new String.buffer;
		buf.add ( address_book.templates [ 'SITE_ADDRESS_SELECT_BOX_INIT' ] );
		rows.iterate ( function ( item )
		{
			item [ 'set_kind' ] = kind;
			item [ 'module' ] = module;
			buf.add ( String.formatDict ( address_book.templates [ 'SITE_ADDRESS_SELECT_BOX_ROW' ], item ) );
		} );
		buf.add ( String.formatDict ( address_book.templates [ 'SITE_ADDRESS_SELECT_BOX_END' ],  { 'module' : module } ) );
	
		$( dest_div, buf.toString() );
	} );
	
};
//}}}
//{{{address_book.select_address = function ( id_address, kind, module )
address_book.select_address = function ( id_address, kind, module )
{
	address_book.dispatchEvent ( "seladdress", null, { 'id_address' : id_address, 'kind' : kind, 'module' : module } );
};
//}}}
//{{{address_book.close_list_box = function ()
address_book.close_list_box = function ()
{
	$( "address_select", "" );
};
//}}}
//{{{address_book._set_category_opts = function ( cback )
address_book._set_category_opts = function ( cback )
{
	var post = { 'action' : 'address_book.ajax.list_categs' };
	address_book.ajax ( post, function ( data  )
	{
		data.categs.iterate ( function ( category )
		{
			var id_cat = category [ 'id' ];
			address_book._category_cache [ id_cat ] = category;
			address_book._category_opts.push ( { value : category [ 'id' ], label : category [ 'text' ]  } );
			category.nodes.iterate ( function ( sub_category )
			{
				address_book._category_opts.push ( { value : sub_category [ 'id' ], label : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + sub_category [ 'text' ] } );
				var id_cat = sub_category [ 'id' ];
				address_book._category_cache [ id_cat ] = sub_category;
			} );
		} );
		if ( cback ) cback();
	} );
}
//}}}
address_book.set_subject_kind = function ( el, cback, fname )
{
	var dict = address_book._profile_get_vals ( fname );
	cback ( dict, true ); 
}
address_book._profile_get_vals = function ( fname )
{
	var f = liwe.form.get ( fname );
	if ( typeof ( f ) == "undefined" ) f = liwe.form5.get ( fname );
	var dict = f.get_values ();
	switch ( dict [ 'subject_kind' ] )
	{
		case "1":
			dict [ 'individual' ] = 1;
			dict [ 'company' ] = 0;	
			break;
		case "2":
			dict [ 'individual' ] = 0;
			dict [ 'company' ] = 1;	
			break;
	};
	return dict;	
};
//{{{address_book.change_stato = function ( el, cback, fname )
address_book.change_stato = function ( el, cback, fname )
{
	
	if ( ! el.value ) return;
	var dict = address_book._profile_get_vals ( fname );
	cback ( dict, true ); 
	
}
//}}}
//{{{address_book.change_prov = function ( el, cback, fname )
address_book.change_prov = function ( el, cback, fname )
{
	
	if ( ! el.value ) return;
	var dict = address_book._profile_get_vals ( fname );
	/*
	var f = liwe.form.get ( fname );
	var dict = f.get_values ();
	*/
	dict [ 'prov' ] = el.value;
	dict [ 'city' ] = null;
	

	address_book.comune_search ( el.value, function () {  cback ( dict, true ) } );
};
//}}}
//{{{address_book.comune_search = function ( prov, cback )
address_book.comune_search = function ( prov, cback )
{
	if ( address_book._loc_opts.get (  prov ) ) 
	{
		if ( cback ) 
		{
			cback (); 
			return;
		}else { return address_book._loc_opts.get (  prov ) };
	}
	var post = { 'action' : 'address_book.ajax.comune_search', 'prov' : prov };
	address_book.ajax ( post, function ( resp )
	{
		var comu_sel_list = address_book._build_sel_list ( resp.get ( 'comu_list', [] ), 'comu_cap', 'comu_cap' );
		address_book._loc_opts [ prov ] = comu_sel_list;
		if ( cback ) cback (); else return comu_sel_list;
		
	} );
}
//}}}
//{{{ change_comu ( elem )
address_book.change_comu = function ( el, cback, fname )
{
	if ( ! el.value ) return;

	var f = liwe.form.get ( fname );
	if ( typeof ( f ) == "undefined" ) f = liwe.form5.get ( fname );

	var dict = address_book._profile_get_vals ( fname );
	
	var prov_val = f.get_value ( "prov" )
	//dict [ 'prov' ] = f.get_element ( 'prov' ).value;
	dict [ 'prov' ] = prov_val;
	dict [ 'city' ] = el.value;
	address_book.cap_search  ( prov_val , el.value, function () { cback ( dict, true ) } );
};
//}}}
//{{{address_book.cap_search = function ( prov, city, cback )
address_book.cap_search = function ( prov, city, cback )
{
	var key = prov+city;
	if ( address_book._loc_opts.get (  key ) ) 
	{
		if ( cback )
		{
			cback (); 
			return;
		} else {
			return address_book._loc_opts.get ( key )
		}
	}
		
	var post = { 'action' : 'address_book.ajax.cap_search', 'prov' : prov, 'city' : city };
	address_book.ajax ( post, function ( resp )
	{
		var cap_sel = address_book._build_sel_cap_list ( resp.get ( 'cap_list', [] ) );
		address_book._loc_opts [ key ] = cap_sel;
		if ( cback ) cback (); else return cap_sel;
	} );
}
//}}}
//{{{ _build_sel_list ( lst, label, value )
address_book._build_sel_list = function ( lst, label, value, cback )
{
	var t, l = lst.length;
	var row;
	var res = [];

	//res.push ( { label : '(Selezionare)', value : 0 } );
	for ( t = 0; t < l; t ++ )
	{
		row = lst [ t ];
		res.push ( { label: row [ label ] , value: row [ value ] } );
	}
	return res;
}
//}}}
//{{{ _build_sel_cap_list ( lst )
address_book._build_sel_cap_list = function ( lst, cback )
{
	var t, l = lst.length;
	var row;
	var res = [];

	for ( t = 0; t < l; t ++ )
	{
		row = lst [ t ];
		res.push ( { label: row.capi_cap +  ' ' + row.fraz_cap, value: row.capi_cap } );
	}

	return res;
};
//}}}
//{{{address_book.get_location_opts = function ( cback, force, data )
address_book.get_location_opts = function ( cback, force, data )
{

	if ( force ) address_book._loc_opts = {};
	var state =  0;
	var state_chk = 2
	var call_cback = function ()
	{
		if ( state < state_chk ) return;
		if ( cback ) cback ( address_book._loc_opts );
	};


	if ( typeof ( data ) == 'undefined' ) data = {};
	if ( data.get ( 'prov', '') != '' )
	{
		state_chk = 3;
		address_book.comune_search 	( data.get ( 'prov' ), 
			function () 
			{
				state += 1;
				call_cback();
			} );
		if ( data.get ( 'city', '') != '' )
		{
			state_chk = 4;
			address_book.cap_search ( data.get ( 'prov' ), data.get ( 'city' ), 
				function () { 
					state += 1;
					call_cback ();
				} );
		}
	}


	if ( address_book._loc_opts.get ( 'stati' ) && address_book._loc_opts.get ( 'province' )  &&  force == null  )
	{
		state += 2;
		call_cback ();
		return;
	};
	var post =[ { 'action' : { 'action' : 'address_book.ajax.list_province' } , name : 'province', label : 'provincia', value : 'sigla' },
				 { 'action' : { 'action' : 'address_book.ajax.list_stati' }, name : 'stati', label : 'stato' , value : 'stato' } ];
	post.iterate ( function ( post )
	{
		var _name = post [ 'name' ];
		var _label = post [ 'label' ];
		var _value = post [ 'value' ];
		address_book._loc_opts [ _name ] = [];
		address_book.ajax ( post [ 'action' ], function ( data )
		{
			
			data[ _name ].iterate ( function ( item )
			{
				address_book._loc_opts [ _name ].push ( { label : item [ _label ], value : item [ _value ] } );
			});
			state += 1;
			call_cback();
		});
		
	});
};
//}}}
//{{{address_book.get_kind_list = function()
address_book.get_kind_list = function()
{
	var get = { 'action' : 'address_book.ajax.list_kind' };
	if ( address_book.kind_list != null ) return;
	address_book.ajax ( get, function ( resp )
	{
		liwe.types.to_list ( resp.kind );
		address_book.kind_list = resp.kind; 
	});
};
//}}}
//{{{address_book.kind_opts = function()
address_book.kind_opts = function()
{
	var res = []
	address_book.kind_list.iterate ( function ( el )
	{
		res.push ( { 'label' : el [ 'descr' ], 'value' : el [ 'id' ] } );
	});
	return res;
};
//}}}
